package com.moyu.oasys.announcement.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.alibaba.nacos.api.model.v2.Result;
import com.github.pagehelper.PageInfo;
import com.moyu.common.core.domain.Page;
import com.moyu.common.core.domain.R;
import com.moyu.oasys.announcement.entity.Announcement;
import com.moyu.oasys.announcement.service.IAnnouncementService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 赵彦博
 * @Date 2024/12/2 19:47
 */
@RestController
@RequestMapping("announcements")
public class AnnouncementController {

    private final IAnnouncementService announcementService;

    public AnnouncementController(IAnnouncementService announcementService) {
        this.announcementService = announcementService;
    }

    @GetMapping("/listAnnouncement")
    public R<Page<Announcement>> listAnnouncement(@RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Announcement> pageInfo = announcementService.list(pageNumber, pageSize);
        return R.ok(Page.of(pageInfo.getList(), pageInfo.getTotal()));
    }

    @SaCheckRole("公告管理员")
    @PostMapping("/createAnnouncement")
    public R<Announcement> createAnnouncement(@RequestBody Announcement announcement) {
        return R.ok(announcementService.create(announcement));
    }

    @SaCheckRole("公告管理员")
    @PutMapping("/updateAnnouncement")
    public R<Announcement> updateAnnouncement(@RequestBody Announcement announcement) {
        Announcement updatedAnnouncement = announcementService.update(announcement);
        return R.ok(updatedAnnouncement);
    }

    @SaCheckRole("公告管理员")
    @DeleteMapping("{id}")
    public R<String> deleteAnnouncement(@PathVariable Long id) {
        announcementService.delete(id);
        return R.ok("删除成功！");
    }

}
